package top.xiaoshuaila.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import top.xiaoshuaila.pojo.Admin;
import top.xiaoshuaila.pojo.RespBean;
import top.xiaoshuaila.service.IAdminService;

import java.util.Map;

@RestController
@Api(tags = "个人中心管理")
public class AdminInfoController {
    @Autowired
    private IAdminService adminService;


    @ApiOperation("更新当前用户信息")
    @PutMapping("/admin/info")
    public RespBean updateAdmin(@RequestBody Admin admin,Authentication authentication){
        System.out.println("ingo");
        if(adminService.updateById(admin)) {
            SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(admin,null,authentication.getAuthorities()));
            return RespBean.success("更新成功!");
        }
        return RespBean.error("更新失败!");
    }

    @ApiOperation("更新用户密码")
    @PutMapping("/admin/pass")
    public RespBean updateAdminPassword (@RequestBody Map<String,Object> info){
        String oldPass=(String)info.get("oldPass");
        String pass=(String)info.get("pass");
        Integer adminId=(Integer)info.get("adminId");
        return adminService.updateAdminPassword(oldPass,pass,adminId);
    }
}
